package com.market.web.controller;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.market.domain.User;
import com.market.manager.UserManager;

@Component("passwordRecoverController")
@Scope("session")
public class PasswordRecoverController implements Serializable {

	@Autowired
	private UserManager userManager;

	@Autowired
	private EmailController emailController;

	/**
	 * 
	 */
	private static final long serialVersionUID = 2358236487103313254L;

	private enum STATUS {
		NOT_CHECKED, EMAIL_CHECKED, EMAIL_CHECKED_INCRORRECT, PASSWORD_ANSWER_CHECKED, PASSWORD_ANSWER_CHECKED_INCRORRECT
	}

	private User user;
	private String email;
	private String passwordQuestion;
	private String passwordAnswer;
	private STATUS status = STATUS.NOT_CHECKED;

	public void init() {
		this.email = null;
		this.passwordQuestion = null;
		this.passwordAnswer = null;
		this.status = STATUS.NOT_CHECKED;

	}

	public void startInputingEmail() {
		this.passwordAnswer = null;
		this.passwordQuestion = null;
		this.status = STATUS.NOT_CHECKED;
	}

	public void startInputingPasswordAnswer() {
		this.status = STATUS.EMAIL_CHECKED;
	}

	public Boolean getIsEmailIncorrect() {
		if (this.status == STATUS.EMAIL_CHECKED_INCRORRECT) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsEmailCorrect() {
		if (this.status == STATUS.EMAIL_CHECKED
				|| this.status == STATUS.PASSWORD_ANSWER_CHECKED
				|| this.status == STATUS.PASSWORD_ANSWER_CHECKED_INCRORRECT) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsPasswordAnswerCorrect() {
		if (this.status == STATUS.PASSWORD_ANSWER_CHECKED) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsPasswordAnswerIncorrect() {
		if (this.status == STATUS.PASSWORD_ANSWER_CHECKED_INCRORRECT) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public void checkEmail() {
		this.user = this.userManager.fetchByEmail(this.email);
		if (this.user != null) {
			this.passwordQuestion = user.getPasswordQuestion();
			this.status = STATUS.EMAIL_CHECKED;
		} else {
			this.passwordQuestion = null;
			this.status = STATUS.EMAIL_CHECKED_INCRORRECT;
		}
	}

	public void checkPasswordAnswer() {
		if (this.passwordAnswer.equalsIgnoreCase(this.user.getPasswordAnswer())) {
			this.status = STATUS.PASSWORD_ANSWER_CHECKED;
			this.emailController.sendEmail(user.getPasswordRemindEmail());		
		} else {
			this.status = STATUS.PASSWORD_ANSWER_CHECKED_INCRORRECT;
		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email.trim();
	}

	public String getPasswordQuestion() {
		return passwordQuestion;
	}

	public void setPasswordQuestion(String passwordQuestion) {
		this.passwordQuestion = passwordQuestion;
	}

	public String getPasswordAnswer() {
		return passwordAnswer;
	}

	public void setPasswordAnswer(String passwordAnswer) {
		this.passwordAnswer = passwordAnswer.trim();
	}

	public STATUS getStatus() {
		return status;
	}

	public void setStatus(STATUS status) {
		this.status = status;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
